package com.hqyj.controller;

import com.hqyj.dao.AdminMapper;
import com.hqyj.pojo.Admin;
import com.hqyj.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author xfy
 * @create 2021-10-24 10:26
 */
@RestController
@RequestMapping("/registerRestful")
public class RegisterRestFulController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminMapper adminMapper;
    @RequestMapping("/register")
    public HashMap<String,Object> register(Admin a, HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<>();
        //获取前端输入的验证码
        String inputEmailCode = a.getEmailCode();
        //?怎么获取已经发送的验证码？使用session
        HttpSession session = request.getSession();
        String sendEmailCode = (String)session.getAttribute("emailCode");
        System.out.println("前端输入的验证码"+inputEmailCode+"    后端生成的验证码"+sendEmailCode);
        if(sendEmailCode.equalsIgnoreCase(inputEmailCode)){
            HashMap<String, Object> add = adminService.add(a);
            Object info = add.get("info");
            if(info=="保存成功"){
                map.put("info","注册成功");
            }else {
                map.put("info",add.get("info"));
            }
        }else {
            map.put("info","验证码输入错误");
        }
        return map;
    }

    @RequestMapping("/updatePassword")
    public HashMap<String,Object> updatePassword(Admin a,HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<>();
        //通过用户名查询邮箱
        List<Admin> list = adminMapper.selectByName(a.getSysName());
        if (list.size()!=0) {
            String email = list.get(0).getEmail();
            //和前端输入的邮件作比较 发送验证码
            if(a.getEmail().equalsIgnoreCase(email)){
                //前端两次输入的确认密码比较
                if(a.getSurePassword().equals(a.getSysPwd())){
                    //获取发送的验证码
                    HttpSession session = request.getSession();
                    String sendEmailCode = (String)session.getAttribute("emailCode");
                    if(a.getEmailCode().equalsIgnoreCase(sendEmailCode)){
                      boolean flag=adminService.updatePassWordByName(a.getSysName(),a.getSysPwd(),list.get(0).getSalt());
                      if(flag){
                          map.put("info","修改成功");
                      }else {
                          map.put("info","修改失败");
                      }
                    }else {
                        map.put("info","验证码输入错误");
                    }

                }else {
                    map.put("info","两次密码不一致");
                }
            }else {
                map.put("info","邮箱错误!非注册邮箱,请重新输入。");
            }
        } else {
            map.put("info","无"+a.getSysName()+"用户");
        }

        return map;
    }
}
